package com.example.a022.myapplication;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
   // private List<View> listViews;
    private ArrayList<Fragment> listViews;
    private ImageView cursor;
    private TextView home,attention,wait;
    private int offset = 0;
    private int currIndex = 0;
    private int bmpW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        init();
        InitImageView();

        listViews = new ArrayList<>();
        LayoutInflater layoutInflater = getLayoutInflater();
//        listViews.add(layoutInflater.inflate(R.layout.home_fragment,null));
//        listViews.add(layoutInflater.inflate(R.layout.home_fragment,null));
//        listViews.add(layoutInflater.inflate(R.layout.home_fragment,null));
//        viewPager.setAdapter(new MyPagerAdapter(listViews));
        listViews.add(new HomeFragment());
        listViews.add(new HomeFragment());
        listViews.add(new HomeFragment());

        TitleFragmentPager2Adapter adapter = new TitleFragmentPager2Adapter(getSupportFragmentManager(),listViews);
        viewPager.setOffscreenPageLimit(4);//ViewPager的缓存为2帧
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);//初始设置ViewPager选中第一帧

//        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());


    }

    private void init() {
        home = (TextView) findViewById(R.id.home);
        attention = (TextView) findViewById(R.id.attention);
        wait = (TextView) findViewById(R.id.wait);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        cursor = (ImageView) findViewById(R.id.cursor);

        home.setOnClickListener(new OnClickListener(0));
        attention.setOnClickListener(new OnClickListener(1));
        wait.setOnClickListener(new OnClickListener(2));
    }

    public class OnClickListener implements View.OnClickListener{
        private int index = 0;

        public OnClickListener(int i){
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }

     private void InitImageView() {
        cursor = (ImageView) findViewById(R.id.cursor);
        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.ic_white)
                 .getWidth();// 获取图片宽度
         DisplayMetrics dm = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = ((screenW-70) / 3 - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
         matrix.postTranslate(offset, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
     }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
        int two = one * 2;// 页卡1 -> 页卡3 偏移量

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(two, 0, 0, 0);
                    }
                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, one, 0, 0);
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(two, one, 0, 0);
                    }
                    break;
                case 2:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, two, 0, 0);
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(one, two, 0, 0);
                    }
                    break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
